﻿using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel;
using System;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;

namespace _20250407
{
    //自定义特性
    public class CustomAttribute : Attribute
    {
        public string Message {  get; set; }
        public CustomAttribute(string message)
        {
            Message = message;
        }
    }
    [AttributeUsage(AttributeTargets.Property)]//限制特性只能应用在属性
    public class BrowsableAttribute : Attribute
    {
        public bool IsBrowsable { get; set; }
        public BrowsableAttribute(bool b)
        {
            this.IsBrowsable = b;
        }
    }
    [Custom("这是应用到类上的自定义特性")]
    public partial class Item()
    {
        public int Id { get; set; }

        [Obsolete("这个属性已过时，使用FullName替代")]//标记已过时的代码
        public string Name { get; set; }

        [Browsable(true)]
        public string FullName { get; set; }
        public string Value { get; set; }

        private DateOnly birthday { get; set; }

        [Custom("这是应用到方法上的自定义特性")]
        public static void Add()//保证类为public，否则反射不到
        {

        }
        [Conditional("DEBUG")]//条件编译，大写
        public void Debug()
        {
            Console.WriteLine("这个方法只在Debug模式下执行");
        }
    }

    class Use
    {
        static Item _item = new Item 
        {
            Id = 1,
            Name = "Xin",
            FullName ="Longkai Xin",
            Value = "Student",
            
        };
        public Use(Item item)
        {
            _item = item;
        }

        static string Serialize(object obj)//序列化
        {
            var res = obj.GetType()
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(pi =>
                {
                    var attr = pi.GetCustomAttribute<BrowsableAttribute>();
                    if (attr != null) return attr.IsBrowsable;
                    return true;
                })
                .Select(pi => new { Key = pi.Name, Value = pi.GetValue(obj) })
                .Select(o=>$"{o.Key}:{o.Value}");
            foreach (var prop in res) 
            {
                Console.WriteLine(prop);
            }
            return "";
        }
        static void Main1(string[] args)
        {
            Serialize(_item);
            Type type = typeof(Item);
            Console.WriteLine("-----------------");
            object[] classAttributes = type.GetCustomAttributes(typeof(CustomAttribute),true);
            foreach (CustomAttribute attribute in classAttributes) 
            {
                Console.WriteLine($"Class Attribute Message:{attribute.Message}");
            }

            MethodInfo methodInfo = type.GetMethod("Add");
            if( methodInfo != null)
            {
                object[] methodAttributes = methodInfo.GetCustomAttributes(typeof(CustomAttribute), true);
                foreach (CustomAttribute attribute in methodAttributes) 
                {
                    Console.WriteLine($"Method Attribute Message:{attribute.Message}");
                }
            }
            else
            {
                Console.WriteLine("没有找到方法");
            }
            PropertyInfo propertyInfo = type.GetProperty("DEBUG");
            if (propertyInfo != null) 
            {
                object[] propertyAttributes = propertyInfo.GetCustomAttributes(typeof(ObsoleteAttribute), true);
                foreach(ObsoleteAttribute attribute in propertyAttributes)
                {
                    Console.WriteLine($"Property Attribute Message:{attribute.Message}");
                }
            }
            else
            {
                Console.WriteLine("没有找到属性");
            }
        }

    }
}
namespace _20250407
{
    class Program
    {
        static void Main1( string[] args)
        {
            // 创建服务集合
            var services = new ServiceCollection();
            // 注册服务
            services.AddTransient<IService, CarService>();
            services.AddTransient<Client>();

            // 构建服务提供者
            //var serviceProvider = services.BuildServiceProvider();

            // 获取 Client 实例
            //var client = serviceProvider.GetService<Client>();
            //client.Run();

        }
    }
    interface IService
    {
        void DoSomething();
    }
    class CarService : IService
    {
        public void DoSomething()
        {
            Console.WriteLine("Car is Running...");
        }
    }
    class PhoneService : IService
    {
        public void DoSomething()
        {
            Console.WriteLine("Phone is calling...");
        }
    }
    class Client
    {
        private readonly IService _service;
        public Client(IService service)
        {
            _service = service;   
        }
        public void Run()
        {
            _service.DoSomething();
        }
    }
}